/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * WorkingPatternMasterEditor.java
 *
 * Created on 10-Jun-2009, 09:58:27
 */
package TimeRecording.forms;

import TimeRecording.business.DaisyChainUtilities;
import TimeRecording.business.HolidayCalculations;
import TimeRecording.business.PeriodCalculations;
import TimeRecording.business.calcs.HolidayAllowance;
import TimeRecording.business.calcs.PatternsSummary;
import TimeRecording.enums.HolidayAdjustmentStatus;
import TimeRecording.enums.HolidayGrantType;
import TimeRecording.tables.HolidayAdjustment;
import TimeRecording.tables.WorkingPattern;
import TimeRecording.tables.WorkingSummary;
import TimeRecording.widgets.DoubleConverter;
import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import javax.swing.JOptionPane;
import org.jdesktop.application.Action;

/**
 *
 * @author cbegg
 */
public class WorkingSummaryEditor extends javax.swing.JDialog {

    private DoubleConverter doubleConverter = new DoubleConverter();

    private EntityManager entityManager;

    /** Creates new form WorkingPatternMasterEditor */
    public WorkingSummaryEditor(EmployeesDialog parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.entityManager = parent.getEntityManager();
    }

    protected boolean workingPatternConfirmed = false;

    /**
     * Get the value of boolean
     *
     * @return the value of boolean
     */
    public boolean isWorkingPatternConfirmed() {
        return workingPatternConfirmed;
    }

    /**
     * Set the value of boolean
     *
     * @param workingPatternConfirmed new value of boolean
     */
    public void setWorkingPatternConfirmed(boolean workingPatternConfirmed) {
        this.workingPatternConfirmed = workingPatternConfirmed;

    }

    protected WorkingSummary currentRecord;

    /**
     * Get the value of currentRecord
     *
     * @return the value of currentRecord
     */
    public WorkingSummary getCurrentRecord() {
        return currentRecord;
    }

    /**
     * Set the value of currentRecord
     *
     * @param currentRecord new value of currentRecord
     */
    public void setCurrentRecord(WorkingSummary currentRecord) {

        WorkingSummary oldRecord = this.currentRecord;
        this.currentRecord = currentRecord;
        firePropertyChange("currentRecord", oldRecord, currentRecord);

        if (currentRecord.getLastAnnualLeaveAllowanceDays() > 0) {
            lastYearHours.setToolTipText(HolidayCalculations.getAnnualAllowanceTip(
                    currentRecord.getLastAnnualLeaveAllowanceDays(),
                    currentRecord.getLastAnnualLeaveAllowanceHours()));
        }

        if (currentRecord.getAnnualLeaveAllowanceDays() > 0) {
            thisYearHours.setToolTipText(HolidayCalculations.getAnnualAllowanceTip(
                    currentRecord.getAnnualLeaveAllowanceDays(),
                    currentRecord.getAnnualLeaveAllowanceHours()));
        }

        if (currentRecord.getNextAnnualLeaveAllowanceDays() > 0) {
            nextYearHours.setToolTipText(HolidayCalculations.getAnnualAllowanceTip(
                    currentRecord.getNextAnnualLeaveAllowanceDays(),
                    currentRecord.getNextAnnualLeaveAllowanceHours()));
        }

        // Set focus to first entry field
        descriptionTextPane.requestFocus();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        employeePanel = new javax.swing.JPanel();
        employeeLabel = new javax.swing.JLabel();
        employeeText = new javax.swing.JTextField();
        descriptionPanel = new javax.swing.JPanel();
        descriptionScrollPanel = new javax.swing.JScrollPane();
        descriptionTextPane = new javax.swing.JTextPane();
        holidayPanelLabel = new javax.swing.JLabel();
        formPanel = new javax.swing.JPanel();
        lastYearPanel = new javax.swing.JPanel();
        lastYearDays = new javax.swing.JTextField();
        lastYearHours = new javax.swing.JTextField();
        calculateLastYearButton = new javax.swing.JButton();
        lastDaysLabel = new javax.swing.JLabel();
        lastHoursLabel = new javax.swing.JLabel();
        approveLastYearButton = new javax.swing.JButton();
        thisYearPanel = new javax.swing.JPanel();
        thisYearDays = new javax.swing.JTextField();
        thisYearHours = new javax.swing.JTextField();
        calculateThisYearButton = new javax.swing.JButton();
        thisDaysLabel = new javax.swing.JLabel();
        thisHoursLabel = new javax.swing.JLabel();
        approveThisYearButton = new javax.swing.JButton();
        nextYearPanel = new javax.swing.JPanel();
        nextYearDays = new javax.swing.JTextField();
        nextYearHours = new javax.swing.JTextField();
        calculateNextYearButton = new javax.swing.JButton();
        nextDaysLabel = new javax.swing.JLabel();
        nextHoursLabel = new javax.swing.JLabel();
        approveNextYearButton = new javax.swing.JButton();
        buttonPanel = new javax.swing.JPanel();
        saveButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(TimeRecording.TimeRecordingApp.class).getContext().getResourceMap(WorkingSummaryEditor.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N
        getContentPane().setLayout(new java.awt.GridBagLayout());

        employeePanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(8, 1, 1, 1));
        employeePanel.setName("employeePanel"); // NOI18N
        employeePanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER, 8, 4));

        employeeLabel.setText(resourceMap.getString("employeeLabel.text")); // NOI18N
        employeeLabel.setName("employeeLabel"); // NOI18N
        employeePanel.add(employeeLabel);

        employeeText.setEditable(false);
        employeeText.setMinimumSize(new java.awt.Dimension(150, 20));
        employeeText.setName("employeeText"); // NOI18N
        employeeText.setPreferredSize(new java.awt.Dimension(250, 20));

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ, this, org.jdesktop.beansbinding.ELProperty.create("${currentRecord.employees}"), employeeText, org.jdesktop.beansbinding.BeanProperty.create("text"));
        binding.setConverter(null);
        bindingGroup.addBinding(binding);

        employeePanel.add(employeeText);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        getContentPane().add(employeePanel, gridBagConstraints);

        descriptionPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("descriptionPanel.border.title"))); // NOI18N
        descriptionPanel.setName("descriptionPanel"); // NOI18N
        descriptionPanel.setLayout(new javax.swing.BoxLayout(descriptionPanel, javax.swing.BoxLayout.PAGE_AXIS));

        descriptionScrollPanel.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createEmptyBorder(0, 6, 5, 6), javax.swing.BorderFactory.createEtchedBorder()));
        descriptionScrollPanel.setName("descriptionScrollPanel"); // NOI18N

        descriptionTextPane.setName("descriptionTextPane"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${currentRecord.patternDescription}"), descriptionTextPane, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        descriptionScrollPanel.setViewportView(descriptionTextPane);

        descriptionPanel.add(descriptionScrollPanel);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 4, 0, 4);
        getContentPane().add(descriptionPanel, gridBagConstraints);

        holidayPanelLabel.setFont(resourceMap.getFont("holidayPanelLabel.font")); // NOI18N
        holidayPanelLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        holidayPanelLabel.setText(resourceMap.getString("holidayPanelLabel.text")); // NOI18N
        holidayPanelLabel.setToolTipText(resourceMap.getString("holidayPanelLabel.toolTipText")); // NOI18N
        holidayPanelLabel.setBorder(javax.swing.BorderFactory.createEmptyBorder(4, 1, 4, 1));
        holidayPanelLabel.setName("holidayPanelLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        getContentPane().add(holidayPanelLabel, gridBagConstraints);

        formPanel.setMinimumSize(new java.awt.Dimension(10, 120));
        formPanel.setName("formPanel"); // NOI18N
        formPanel.setPreferredSize(new java.awt.Dimension(10, 148));
        formPanel.setLayout(new java.awt.GridBagLayout());

        lastYearPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("lastYearPanel.border.title"))); // NOI18N
        lastYearPanel.setName("lastYearPanel"); // NOI18N
        lastYearPanel.setPreferredSize(new java.awt.Dimension(220, 140));
        lastYearPanel.setLayout(new java.awt.GridBagLayout());

        lastYearDays.setName("lastYearDays"); // NOI18N
        lastYearDays.setPreferredSize(new java.awt.Dimension(60, 20));

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${currentRecord.lastAnnualLeaveAllowanceDays}"), lastYearDays, org.jdesktop.beansbinding.BeanProperty.create("text"));
        binding.setConverter(doubleConverter);
        bindingGroup.addBinding(binding);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        lastYearPanel.add(lastYearDays, gridBagConstraints);

        lastYearHours.setName("lastYearHours"); // NOI18N
        lastYearHours.setPreferredSize(new java.awt.Dimension(60, 20));

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${currentRecord.lastAnnualLeaveAllowanceHours}"), lastYearHours, org.jdesktop.beansbinding.BeanProperty.create("text"));
        binding.setConverter(doubleConverter);
        bindingGroup.addBinding(binding);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        lastYearPanel.add(lastYearHours, gridBagConstraints);

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(TimeRecording.TimeRecordingApp.class).getContext().getActionMap(WorkingSummaryEditor.class, this);
        calculateLastYearButton.setAction(actionMap.get("recalculateLastYear")); // NOI18N
        calculateLastYearButton.setText(resourceMap.getString("calculateLastYearButton.text")); // NOI18N
        calculateLastYearButton.setMaximumSize(new java.awt.Dimension(200, 23));
        calculateLastYearButton.setName("calculateLastYearButton"); // NOI18N
        calculateLastYearButton.setPreferredSize(new java.awt.Dimension(113, 23));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        lastYearPanel.add(calculateLastYearButton, gridBagConstraints);

        lastDaysLabel.setText(resourceMap.getString("lastDaysLabel.text")); // NOI18N
        lastDaysLabel.setName("lastDaysLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        lastYearPanel.add(lastDaysLabel, gridBagConstraints);

        lastHoursLabel.setText(resourceMap.getString("lastHoursLabel.text")); // NOI18N
        lastHoursLabel.setName("lastHoursLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        lastYearPanel.add(lastHoursLabel, gridBagConstraints);

        approveLastYearButton.setAction(actionMap.get("grantLastYear")); // NOI18N
        approveLastYearButton.setText(resourceMap.getString("approveLastYearButton.text")); // NOI18N
        approveLastYearButton.setName("approveLastYearButton"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        lastYearPanel.add(approveLastYearButton, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        formPanel.add(lastYearPanel, gridBagConstraints);

        thisYearPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("thisYearPanel.border.title"))); // NOI18N
        thisYearPanel.setName("thisYearPanel"); // NOI18N
        thisYearPanel.setPreferredSize(new java.awt.Dimension(220, 140));
        thisYearPanel.setLayout(new java.awt.GridBagLayout());

        thisYearDays.setName("thisYearDays"); // NOI18N
        thisYearDays.setPreferredSize(new java.awt.Dimension(60, 20));

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${currentRecord.annualLeaveAllowanceDays}"), thisYearDays, org.jdesktop.beansbinding.BeanProperty.create("text"));
        binding.setConverter(doubleConverter);
        bindingGroup.addBinding(binding);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        thisYearPanel.add(thisYearDays, gridBagConstraints);

        thisYearHours.setName("thisYearHours"); // NOI18N
        thisYearHours.setPreferredSize(new java.awt.Dimension(60, 20));

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${currentRecord.annualLeaveAllowanceHours}"), thisYearHours, org.jdesktop.beansbinding.BeanProperty.create("text"));
        binding.setConverter(doubleConverter);
        bindingGroup.addBinding(binding);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        thisYearPanel.add(thisYearHours, gridBagConstraints);

        calculateThisYearButton.setAction(actionMap.get("recalculateThisYear")); // NOI18N
        calculateThisYearButton.setText(resourceMap.getString("calculateThisYearButton.text")); // NOI18N
        calculateThisYearButton.setMaximumSize(new java.awt.Dimension(200, 23));
        calculateThisYearButton.setName("calculateThisYearButton"); // NOI18N
        calculateThisYearButton.setPreferredSize(new java.awt.Dimension(113, 23));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        thisYearPanel.add(calculateThisYearButton, gridBagConstraints);

        thisDaysLabel.setText(resourceMap.getString("thisDaysLabel.text")); // NOI18N
        thisDaysLabel.setName("thisDaysLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        thisYearPanel.add(thisDaysLabel, gridBagConstraints);

        thisHoursLabel.setText(resourceMap.getString("thisHoursLabel.text")); // NOI18N
        thisHoursLabel.setName("thisHoursLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        thisYearPanel.add(thisHoursLabel, gridBagConstraints);

        approveThisYearButton.setAction(actionMap.get("grantThisYear")); // NOI18N
        approveThisYearButton.setText(resourceMap.getString("approveThisYearButton.text")); // NOI18N
        approveThisYearButton.setName("approveThisYearButton"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        thisYearPanel.add(approveThisYearButton, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        formPanel.add(thisYearPanel, gridBagConstraints);

        nextYearPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("nextYearPanel.border.title"))); // NOI18N
        nextYearPanel.setName("nextYearPanel"); // NOI18N
        nextYearPanel.setPreferredSize(new java.awt.Dimension(220, 140));
        nextYearPanel.setLayout(new java.awt.GridBagLayout());

        nextYearDays.setName("nextYearDays"); // NOI18N
        nextYearDays.setPreferredSize(new java.awt.Dimension(60, 20));

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${currentRecord.nextAnnualLeaveAllowanceDays}"), nextYearDays, org.jdesktop.beansbinding.BeanProperty.create("text"));
        binding.setConverter(doubleConverter);
        bindingGroup.addBinding(binding);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        nextYearPanel.add(nextYearDays, gridBagConstraints);

        nextYearHours.setName("nextYearHours"); // NOI18N
        nextYearHours.setPreferredSize(new java.awt.Dimension(60, 20));

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${currentRecord.nextAnnualLeaveAllowanceHours}"), nextYearHours, org.jdesktop.beansbinding.BeanProperty.create("text"));
        binding.setConverter(doubleConverter);
        bindingGroup.addBinding(binding);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        nextYearPanel.add(nextYearHours, gridBagConstraints);

        calculateNextYearButton.setAction(actionMap.get("recalculateNextYear")); // NOI18N
        calculateNextYearButton.setText(resourceMap.getString("calculateNextYearButton.text")); // NOI18N
        calculateNextYearButton.setMaximumSize(new java.awt.Dimension(200, 23));
        calculateNextYearButton.setName("calculateNextYearButton"); // NOI18N
        calculateNextYearButton.setPreferredSize(new java.awt.Dimension(113, 23));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        nextYearPanel.add(calculateNextYearButton, gridBagConstraints);

        nextDaysLabel.setText(resourceMap.getString("nextDaysLabel.text")); // NOI18N
        nextDaysLabel.setName("nextDaysLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        nextYearPanel.add(nextDaysLabel, gridBagConstraints);

        nextHoursLabel.setText(resourceMap.getString("nextHoursLabel.text")); // NOI18N
        nextHoursLabel.setName("nextHoursLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        nextYearPanel.add(nextHoursLabel, gridBagConstraints);

        approveNextYearButton.setAction(actionMap.get("grantNextYear")); // NOI18N
        approveNextYearButton.setText(resourceMap.getString("approveNextYearButton.text")); // NOI18N
        approveNextYearButton.setName("approveNextYearButton"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        nextYearPanel.add(approveNextYearButton, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        formPanel.add(nextYearPanel, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 4, 0, 4);
        getContentPane().add(formPanel, gridBagConstraints);

        buttonPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 0, 5, 0));
        buttonPanel.setMaximumSize(new java.awt.Dimension(32767, 36));
        buttonPanel.setMinimumSize(new java.awt.Dimension(146, 36));
        buttonPanel.setName("buttonPanel"); // NOI18N
        buttonPanel.setPreferredSize(new java.awt.Dimension(148, 36));
        buttonPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER, 5, 0));

        saveButton.setAction(actionMap.get("saveClicked")); // NOI18N
        saveButton.setText(resourceMap.getString("saveButton.text")); // NOI18N
        saveButton.setName("saveButton"); // NOI18N
        buttonPanel.add(saveButton);

        cancelButton.setAction(actionMap.get("cancelClicked")); // NOI18N
        cancelButton.setText(resourceMap.getString("cancelButton.text")); // NOI18N
        cancelButton.setName("cancelButton"); // NOI18N
        buttonPanel.add(cancelButton);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        getContentPane().add(buttonPanel, gridBagConstraints);

        bindingGroup.bind();

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-549)/2, (screenSize.height-486)/2, 549, 486);
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton approveLastYearButton;
    private javax.swing.JButton approveNextYearButton;
    private javax.swing.JButton approveThisYearButton;
    private javax.swing.JPanel buttonPanel;
    private javax.swing.JButton calculateLastYearButton;
    private javax.swing.JButton calculateNextYearButton;
    private javax.swing.JButton calculateThisYearButton;
    private javax.swing.JButton cancelButton;
    private javax.swing.JPanel descriptionPanel;
    private javax.swing.JScrollPane descriptionScrollPanel;
    private javax.swing.JTextPane descriptionTextPane;
    private javax.swing.JLabel employeeLabel;
    private javax.swing.JPanel employeePanel;
    private javax.swing.JTextField employeeText;
    private javax.swing.JPanel formPanel;
    private javax.swing.JLabel holidayPanelLabel;
    private javax.swing.JLabel lastDaysLabel;
    private javax.swing.JLabel lastHoursLabel;
    private javax.swing.JTextField lastYearDays;
    private javax.swing.JTextField lastYearHours;
    private javax.swing.JPanel lastYearPanel;
    private javax.swing.JLabel nextDaysLabel;
    private javax.swing.JLabel nextHoursLabel;
    private javax.swing.JTextField nextYearDays;
    private javax.swing.JTextField nextYearHours;
    private javax.swing.JPanel nextYearPanel;
    private javax.swing.JButton saveButton;
    private javax.swing.JLabel thisDaysLabel;
    private javax.swing.JLabel thisHoursLabel;
    private javax.swing.JTextField thisYearDays;
    private javax.swing.JTextField thisYearHours;
    private javax.swing.JPanel thisYearPanel;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

    private float patternHours = 0;

    void setPatternHours(float patternHoursThisWeek) {
        this.patternHours = patternHoursThisWeek;
    }

    @Action
    public void saveClicked() {
        if (currentRecord.getHoursWorkedPerWeek() > 0 && patternHours > 0) {
            if (JOptionPane.showConfirmDialog(this,
                    "Paid sessions are present for this week.\n" +
                    "Do you also wish pay contracted time too",
                    "Confirm", JOptionPane.OK_CANCEL_OPTION)
                    == JOptionPane.CANCEL_OPTION) {
                return;
            }
        }
        setWorkingPatternConfirmed(true);
        setVisible(false);
    }

    @Action
    public void cancelClicked() {
        setWorkingPatternConfirmed(false);
        setVisible(false);
    }

    @Action
    public void recalculateLastYear() {

        PatternsSummary patternsSummary = WorkingPattern.
                calculatePatternsSummary(entityManager,
                        currentRecord.getEmployees(),
                        DaisyChainUtilities.getLastSystemYear());

        HolidayAllowance allowance = HolidayCalculations.
                calculateAnnualAllowance(patternsSummary);

        currentRecord.setLastAnnualLeaveAllowanceHours(allowance.getHoursLeave());
        currentRecord.setLastAnnualLeaveAllowanceDays(allowance.getDaysLeave());
        lastYearHours.setToolTipText(allowance.getExplanation());
    }

    @Action
    public void recalculateThisYear() {

        PatternsSummary patternsSummary = WorkingPattern.
                calculatePatternsSummary(entityManager,
                        currentRecord.getEmployees(),
                        DaisyChainUtilities.getThisSystemYear());

        HolidayAllowance allowance = HolidayCalculations.
                calculateAnnualAllowance(patternsSummary);

        currentRecord.setAnnualLeaveAllowanceHours(allowance.getHoursLeave());
        currentRecord.setAnnualLeaveAllowanceDays(allowance.getDaysLeave());
        thisYearHours.setToolTipText(allowance.getExplanation());
    }
    
    @Action
    public void recalculateNextYear() {

        PatternsSummary patternsSummary = WorkingPattern.
                calculatePatternsSummary(entityManager,
                        currentRecord.getEmployees(),
                        DaisyChainUtilities.getNextSystemYear());

        HolidayAllowance allowance = HolidayCalculations.
                calculateAnnualAllowance(patternsSummary);

        currentRecord.setNextAnnualLeaveAllowanceHours(allowance.getHoursLeave());
        currentRecord.setNextAnnualLeaveAllowanceDays(allowance.getDaysLeave());
        nextYearHours.setToolTipText(allowance.getExplanation());
    }

    private void makeAnnualGrant(Date startOfYear, double hoursGranted) {
        
        try {
            entityManager.getTransaction().begin();

            List<HolidayAdjustment> adjustments = HolidayAdjustment.
                    getHolidayAdjustmentsForEmployeeDateAndType(
                    entityManager, currentRecord.getEmployees(),
                    startOfYear, HolidayGrantType.ANNUAL);

            HolidayAdjustment holidayGrant;
            if (adjustments.isEmpty()) {
                holidayGrant = new HolidayAdjustment();
                holidayGrant.setAdjustmentType(HolidayGrantType.ANNUAL);
                holidayGrant.setStatus(HolidayAdjustmentStatus.RELEASED);
                holidayGrant.setEmployeeId(currentRecord.getEmployees());
                holidayGrant.setAdjustmentDate(startOfYear);
            } else {
                holidayGrant = adjustments.get(0);
            }

            holidayGrant.setHoursAdjusted(hoursGranted);

            entityManager.persist(holidayGrant);
            entityManager.getTransaction().commit();

        } catch (RuntimeException e) {
            entityManager.getTransaction().rollback();
            ErrorDialog.showError(this, e);
        }
    }

    @Action
    public void grantLastYear() {
        makeAnnualGrant(
                DaisyChainUtilities.getLastSystemYear().getFromDate(),
                currentRecord.getLastAnnualLeaveAllowanceHours());
    }

    @Action
    public void grantThisYear() {
        makeAnnualGrant(
                DaisyChainUtilities.getThisSystemYear().getFromDate(),
                currentRecord.getAnnualLeaveAllowanceHours());
    }

    @Action
    public void grantNextYear() {
        makeAnnualGrant(
                DaisyChainUtilities.getNextSystemYear().getFromDate(),
                currentRecord.getNextAnnualLeaveAllowanceHours());
    }
}
